package com.oristand.sqlmonitor.controller;

import com.oristand.sqlmonitor.config.DynamicDataSourceUtil;
import com.oristand.sqlmonitor.config.DynamicDatasource;
import com.oristand.sqlmonitor.entity.DbEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 数据源controller
 *
 * @author wangmeng
 * @since 2023/7/5
 */
@RestController
@RequestMapping("datasource")
public class DatasourceController {

    @Autowired
    DynamicDatasource datasource;

    @GetMapping("test")
    public String test(){
        DynamicDataSourceUtil.setDataSource("osj_expense_sit");
        JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from t_apply_info");
        System.out.println(1);
        return "1";
    }


    @GetMapping("testAdd")
    public String testAdd(){
        DbEntity entity=new DbEntity();
        entity.setDriverName("com.mysql.cj.jdbc.Driver");
        entity.setPassword("Yanfa.0420@osj");
        entity.setUsername("osj_tester");
        entity.setDatabaseName("purchase_receipt");
        entity.setJdbcUrl("jdbc:mysql://192.168.20.220:3306/purchase_receipt_sit?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2b8");
        datasource.addDataSourceBeans(Collections.singletonList(entity));
        JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
        DynamicDataSourceUtil.setDataSource("purchase_receipt");
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from m_user");
        System.out.println(1);
        return "1";
    }

}
